package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    //注入RoleService
    @Reference
    private RoleService roleService;


    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list= roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, null);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }
    /*
        分页查询所有角色信息
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = roleService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }

    /*
        添加角色,关联角色和权限表
     */
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @RequestMapping("/addPerAndRole")
    public Result addPerAndRole(@RequestBody Role role, Integer[] permissionIds) {
        try {
            //调用RoleService
            roleService.addPerAndRole(role, permissionIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /*
        关联菜单和角色表
     */
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @RequestMapping("/addMenuAndRole")
    public Result addMenuAndRole(@RequestBody Role role, Integer[] menuIds) {

        try {
            //调用RoleService
            roleService.addMenuAndRole(role, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /*
        根据角色id查询角色信息
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findById")
    public Result findById(Integer roleId) {
        try {
            //调用服务层
            Role role = roleService.findById(roleId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /*
        根据角色id查询对应的权限id
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findPerIdByRoleId")
    public Result findPerIdByRoleId(Integer roleId) {
        try {
            //调用服务层
            Integer[] permissionIds = roleService.findPerIdByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /*
        根据角色id查询对应的菜单id
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @RequestMapping("/findMenuIdByRoleId")
    public Result findMenuIdByRoleId(Integer roleId) {
        try {
            //调用服务层
            Integer[] checkedMenuIds = roleService.findMenuIdByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkedMenuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /*
        修改角色,修改角色和权限关联的表
     */
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @RequestMapping("/editPerAndRole")
    public Result editPerAndRole(@RequestBody Role role, Integer[] permissionIds) {
        try {
            //调用服务层
            roleService.editPerAndRole(role, permissionIds);
            return new Result(true, MessageConstant.EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_FAIL);
        }
    }

    /*
        修改角色和菜单关联的表
     */
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @RequestMapping("/editMenuAndRole")
    public Result editMenuAndRole(@RequestBody Role role, Integer[] menuIds) {
        try {
            //调用服务层
            roleService.editMenuAndRole(role.getId(), menuIds);
            return new Result(true, MessageConstant.EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_FAIL);
        }
    }

    /*
        删除角色
     */
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer role_id) {
        try {
            //调用服务层
            roleService.delete(role_id);
            return new Result(true, MessageConstant.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_FAIL);
        }
    }
}
